<template>
  <!-- 知识类型审核弹窗 -->
  <el-dialog
    title="审核"
    :visible.sync="dialogVisible"
    width="25%"
    :show-close="false"
    :before-close="closeDailog"
    center
  >
    <div class="buttons-center">
      <i />
      <Button type="primary" :loading="loading" @click="refuse">审核不通过</Button>
      <Button type="primary" :loading="loading" @click="adopt">审核通过</Button>
      <i />
    </div>
    <FormContainer :column="1">
      <el-form ref="form" :model="formData" :rules="formRules" label-width="40px">
        <el-form-item prop="auditRemarks" label="备注">
          <el-input v-model="formData.auditRemarks" type="textarea" rows="5" clearable />
        </el-form-item>
      </el-form>
    </FormContainer>
  </el-dialog>
</template>
<script>
import FormContainer from '@/components/FormContainer'
import Button from '@/components/Button'
import { knowledgeBaseAudit } from '@/api/knowledgeBase'
export default {
  name: 'Examine',
  components: {
    FormContainer,
    Button
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      formData: {
        auditRemarks: ''
      },
      curData: {},
      formRules: {
        auditRemarks: {
          required: true,
          message: '请输入审核备注',
          trigger: 'blur'
        }
      }
    }
  },
  methods: {
    // 打开弹窗
    openDailog(data) {
      this.dialogVisible = true
      this.curData = data
    },
    closeDailog() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    // 通过
    adopt() {
      this.audit({ id: this.curData.id, state: 2 })
    },
    // 不通过
    refuse() {
      this.audit({ id: this.curData.id, state: 3 })
    },
    audit(data) {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          data.auditRemarks = this.formData.auditRemarks
          knowledgeBaseAudit(data).then(res => {
            this.loading = false
            if (res.success === 1) {
              this.$message({
                message: '审核完成!',
                type: 'success'
              })
              this.closeDailog()
              this.$emit('confrim')
            }
          }).catch(er => {
            this.loading = false
          })
        }
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.buttons-center {
  display: flex;
  margin-bottom: 20px;
  i {
    flex-grow: 1;
  }
}
</style>
